package cn.wolfcode.wolf2w.strategy.service.impl;

import cn.wolfcode.wolf2w.strategy.api.domain.Strategy;
import cn.wolfcode.wolf2w.strategy.api.domain.StrategyCondition;
import cn.wolfcode.wolf2w.strategy.api.domain.StrategyRank;
import cn.wolfcode.wolf2w.strategy.mapper.StrategyConditionMapper;
import cn.wolfcode.wolf2w.strategy.query.StrategyConditionQuery;
import cn.wolfcode.wolf2w.strategy.service.IStrategyConditionService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.xml.crypto.Data;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 攻略条件Service业务层处理
 * 
 * @author dalang
 * @date 2023-11-26
 */
@Service
@Transactional
public class StrategyConditionServiceImpl extends ServiceImpl<StrategyConditionMapper,StrategyCondition> implements IStrategyConditionService {
    @Autowired
    private StrategyServiceImpl strategyService;

    @Override
    public IPage<StrategyCondition> queryPage(StrategyConditionQuery qo) {
        IPage<StrategyCondition> page = new Page<>(qo.getCurrentPage(), qo.getPageSize());
        return lambdaQuery()
                .page(page);
    }

    @Override
    public List<StrategyCondition> condition(int type) {
        List<StrategyCondition> list = lambdaQuery()
                .eq(StrategyCondition::getType,type)
                .inSql(StrategyCondition::getStatisTime,"(SELECT MAX(statis_time) from ta_strategy_condition where type = "+type+")")
                .orderByDesc(StrategyCondition::getCount)
                .list();
        return list;
    }

    public void strategyConditionHandle(int type) {
//        select theme_id refid, theme_name name, COUNT(id) count
//        from ta_strategy
//        GROUP BY theme_id, theme_name
//        ORDER BY count DESC
        Date date = new Date();
        List<StrategyCondition> list = new ArrayList<>();
        QueryWrapper<Strategy> wrapper = new QueryWrapper<>();
        if(type == StrategyCondition.TYPE_THEME){
            wrapper.select("theme_id refid, theme_name name, COUNT(id) count");
            wrapper.groupBy("theme_id, theme_name");
        }else{
            wrapper.select("dest_id refid, dest_name name, COUNT(id) count");
            wrapper.eq("isabroad",type == StrategyCondition.TYPE_CHINA ? Strategy.ABROAD_NO:Strategy.ABROAD_YES);
            wrapper.groupBy("dest_id, dest_name");
        }
        wrapper.orderByDesc("count");
        List<Map<String, Object>> maps = strategyService.listMaps(wrapper);
        for (Map<String, Object> map : maps) {
            StrategyCondition sc = new StrategyCondition();
            sc.setName(map.get("name").toString());
            sc.setRefid(Long.valueOf(map.get("refid").toString()));
            sc.setType(type);
            sc.setCount(Integer.parseInt(map.get("count").toString()));
            sc.setStatisTime(date);

            list.add(sc);
        }
        super.saveBatch(list);
    }

    @Override
    public void strategyConditionDelete() {
        Calendar start = Calendar.getInstance();
        start.set(Calendar.HOUR_OF_DAY, 0);
        start.set(Calendar.MINUTE, 0);
        start.set(Calendar.SECOND, 0);
        start.set(Calendar.MILLISECOND, 0);

        // 获取今天的23点59分
        Calendar end = Calendar.getInstance();
        end.set(Calendar.HOUR_OF_DAY, 23);
        end.set(Calendar.MINUTE, 59);
        end.set(Calendar.SECOND, 59);
        end.set(Calendar.MILLISECOND, 999);

        QueryWrapper<StrategyCondition> wrapper = new QueryWrapper<>();
//        wrapper.inSql("DATE(statis_time)","CURDATE()");
        wrapper.gt("statis_time", start);
        wrapper.lt("statis_time", end);
        wrapper.notInSql("statis_time", "(SELECT MAX(statis_time) from ta_strategy_condition where DATE(statis_time) = CURDATE())");
        List<StrategyCondition> list = super.list(wrapper);
//        list.stream().map(StrategyRank::getId).collect(Collectors.toList())
        super.removeByIds(list.stream().map(StrategyCondition::getId).collect(Collectors.toList()));
    }
}
